import { Component, OnInit, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';
import {
    MachineHourServiceProxy,
    MachineHourOfYearListDto,
    DeviceServiceProxy
} from '@shared/service-proxies/service-proxies';
//import { CreateOrEditPowerModalComponent } from './create-or-edit-power-modal.component';
//import { ExcelModalComponent } from '@app/shared/common/excel-modal/excel-modal.component';
import * as moment from 'moment';
import { BsDatepickerViewMode } from 'ngx-bootstrap/datepicker/models';
import { BsDatepickerConfig, BsDaterangepickerConfig } from 'ngx-bootstrap';
import { CreateOrEditMachineHourModalComponent } from './create-or-edit-machine-hour-modal.component';

@Component({
    templateUrl: './machine-hour.component.html',
    animations: [appModuleAnimation()]
})
export class MachineHourComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    // @ViewChild('excelModal') excelModal: ExcelModalComponent;
    @ViewChild('createOrEditMachineHourModal') createOrEditMachineHourModal: CreateOrEditMachineHourModalComponent;

    anyncing = false;
    filterText = '';
    deviceId?: number;
    deviceName = '';
    deviceNumber = '';
    public dateRange: Date[] = [
        moment()
            .startOf('day')
            .toDate(),
        moment()
            .endOf('day')
            .toDate()
    ];
    // userDataPermissions;
    // selectedDepts;
    selectedValues = new Array<MachineHourOfYearListDto>();
    // minMode: BsDatepickerViewMode = 'month';

    // bsConfig: Partial<BsDaterangepickerConfig>;
    // extensions: Array<ListDtoExtensionsItem>;
    constructor(
        injector: Injector,
        private _machineHourServiceProxy: MachineHourServiceProxy,
        private _deviceServiceProxy: DeviceServiceProxy
    ) {
        super(injector);
    }

    ngOnInit() {}

    getTableData(event?: LazyLoadEvent) {
        this.primengTableHelper.showLoadingIndicator();

        this._machineHourServiceProxy
            .getAllAsync(
                this.deviceId,
                this.filterText,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }

    getAllAsync(event?: LazyLoadEvent) {
        this.primengTableHelper.showLoadingIndicator();
        this.filterText = '';
        this.deviceNumber = '';
        this.deviceName = '';
        this._machineHourServiceProxy
            .getAllAsync(
                this.deviceId,
                this.filterText,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    createOrEditMachineHour(): void {
        this.createOrEditMachineHourModal.show();
    }
    delete(MachineHourOfYear: MachineHourOfYearListDto) {
        this.message.confirm('确认删除该条数据吗？', isConfirmed => {
            let self = this;
            if (isConfirmed) {
                this._machineHourServiceProxy.deleteAsync(MachineHourOfYear.id).subscribe(() => {
                    this.notify.success(this.l('SuccessfullyDeleted'));
                    this.getAllAsync();
                });
            }
        });
    }
    queryDevice(event) {
        //let deviceIdQuery: number;
        this._deviceServiceProxy
            .getAllAsync(this.filterText, this.deviceName, this.deviceNumber, '', 1000, 0)
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();

                //this.deviceId = result.items[0].id;
                let deviceId = result.items[0].id;
                this._machineHourServiceProxy
                    .getAllAsync(deviceId, this.filterText, this.primengTableHelper.getSorting(this.dataTable), 1000, 0)
                    .subscribe(result => {
                        this.primengTableHelper.totalRecordsCount = result.totalCount;
                        this.primengTableHelper.records = result.items;
                        this.primengTableHelper.hideLoadingIndicator();
                    });
            });
    }
}
